package edu.unlv.cs.ladders.entities;

import edu.unlv.cs.ladders.entities.BaseEntity;
import java.lang.Long;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;

privileged aspect BaseEntity_Roo_Entity {
    
    @PersistenceContext
    transient EntityManager BaseEntity.entityManager;
    
    @Transactional
    public void BaseEntity.persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }
    
    @Transactional
    public void BaseEntity.remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            BaseEntity attached = this.entityManager.find(BaseEntity.class, this.id);
            this.entityManager.remove(attached);
        }
    }
    
    @Transactional
    public void BaseEntity.flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }
    
    @Transactional
    public void BaseEntity.merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        BaseEntity merged = this.entityManager.merge(this);
        this.entityManager.flush();
        this.id = merged.getId();
    }
    
    public static final EntityManager BaseEntity.entityManager() {
        EntityManager em = new BaseEntity(){
        }.entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }
    
    public static long BaseEntity.countBaseEntitys() {
        return (Long) entityManager().createQuery("select count(o) from BaseEntity o").getSingleResult();
    }
    
    public static List<BaseEntity> BaseEntity.findAllBaseEntitys() {
        return entityManager().createQuery("select o from BaseEntity o").getResultList();
    }
    
    public static BaseEntity BaseEntity.findBaseEntity(Long id) {
        if (id == null) return null;
        return entityManager().find(BaseEntity.class, id);
    }
    
    public static List<BaseEntity> BaseEntity.findBaseEntityEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("select o from BaseEntity o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }
    
}
